Shader "Unlit/UnlitArmyLine"
{
    Properties
    {
        [Enum(UnityEngine.Rendering.CompareFunction)] _ZTestMode ("Z TestMode", Float) = 4
        [Enum(Off,0,On,1)] _ZWriteMode ("Z WriteMode", Float) = 0
        [Enum(UnityEngine.Rendering.CullMode)] _CullMode ("Cull Mode", Float) = 2
        [HDR] _MainTexColor ("MainTex Color", Color) = (1, 1, 1, 1)
        _MainTexture ("MainTexture 主贴图/正面颜色/背面颜色", 2D) = "white" {}
        _MainTex_UVTiling_UVSpeed ("MainTex_UVTiling_UVSpeed", Vector) = (1, 1, 0, 0)
        _Center ("Center", Vector) = (0, 0, 0, 1)
    }

    SubShader
    {
        Tags
        {
            "RenderType" = "Transparent"
            "Queue" = "Transparent"
            "RenderPipeline" = "UniversalPipeline"
        }

        Blend SrcAlpha OneMinusSrcAlpha
        Cull [_CullMode]
        ZWrite [_ZWriteMode]
        ZTest [_ZTestMode]

        Pass
        {
            Name "Unlit"
            Tags
            {
                "LightMode" = "UniversalForward"
            }
            
            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma target 4.5
            
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
            #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
            #include "GPUArmyLine.hlsl"

            float _ZTestMode;
            float _ZWriteMode;
            float _Dst;
            float _CullMode;
            float _Src;
            float4 _Center;

            float4 _MainTexColor;
            sampler2D _MainTexture;
            float4 _MainTex_UVTiling_UVSpeed;

            struct a2v
            {
                uint vertexID : SV_VertexID;
                uint instanceID : SV_InstanceID;
            };

            struct v2f
            {
                float4 pos : SV_POSITION;
                float4 uv : TEXCOORD0;
                float dis : TEXCOORD1;
                uint instanceID : SV_InstanceID;
            };

            v2f vert(a2v v)
            {
                v2f o;
                const float4 position = GetLinePos(v.instanceID, v.vertexID);
                const float2 uv = GetLineUV(v.vertexID);
                const float3 worldPos = position;
                o.uv.xy = uv.xy;
                o.uv.zw = uv.xy;

                const float3 center = mul(GetLineTrs(v.instanceID), _Center).xyz;
                o.dis = distance(center, worldPos) * 0.1;
                o.pos = TransformObjectToHClip(position);
                o.instanceID = v.instanceID;
                return o;
            }

            half4 frag(v2f i) : SV_Target
            {
                float2 mainTexUV = float2(i.dis * _MainTex_UVTiling_UVSpeed.x, i.uv.y * _MainTex_UVTiling_UVSpeed.y) + _Time.y * _MainTex_UVTiling_UVSpeed.zw;
                float4 mainTex = tex2D(_MainTexture, mainTexUV);
                float4 finalColor = mainTex * GetLineColor(i.instanceID);
                finalColor.a = saturate(mainTex.a * _MainTexColor.a);
                return finalColor;
            }
            ENDHLSL
        }
    }
}
